package com.easy.facilitaterecycleadapter.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Sigthp-P-Department on 2018/5/8.
 */

public class WrapRecyclerView extends RecyclerView {
    private AdapterDataObserver dataObserver=new AdapterDataObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            if(mAdapter==null)return;

            mAdapter.notifyDataSetChanged();
            dataChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            super.onItemRangeChanged(positionStart, itemCount);
            if(mAdapter==null)return;
            mAdapter.notifyItemChanged(positionStart);
            dataChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            super.onItemRangeChanged(positionStart, itemCount, payload);
            if(mAdapter==null)return;
            mAdapter.notifyItemChanged(positionStart,payload);
            dataChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            if(mAdapter==null)return;
            mAdapter.notifyItemInserted(positionStart);
            dataChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            if(mAdapter==null)return;
            mAdapter.notifyItemRemoved(positionStart);
            dataChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            super.onItemRangeMoved(fromPosition, toPosition, itemCount);
            if(mAdapter==null)return;
            mAdapter.notifyItemMoved(fromPosition,toPosition);
            dataChanged();
        }
    };

    private WrapViewAdapter mAdapter;

    private View mEmptyView,mLoadingView;

    public WrapRecyclerView(Context context) {
        this(context,null);
    }

    public WrapRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public WrapRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        if(adapter instanceof WrapViewAdapter){
            mAdapter= (WrapViewAdapter) adapter;
        }else{
            mAdapter = new WrapViewAdapter(adapter);
            adapter.registerAdapterDataObserver(dataObserver);
        }
        super.setAdapter(mAdapter);
    }
    public void addHanderView(View view){
        if(mAdapter != null){
            mAdapter.addHanderView(view);
        }else{
        }
    }
    public void addFooterView(View view){
        if(mAdapter != null){
            mAdapter.addFooterView(view);
        }
    }
    public void removeHanderView(View view){
        if(mAdapter != null){
            mAdapter.removeHanderView(view);
        }
    }
    public void removeFooterView(View view){
        if(mAdapter != null){
            mAdapter.removeFooterView(view);
        }
    }

    /**
     * 添加一个空列表数据页面
     */
    public void addEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
    }

    /**
     * 添加一个正在加载数据的页面
     */
    public void addLoadingView(View loadingView) {
        this.mLoadingView = loadingView;
    }

    /**
     * Adapter数据改变的方法
     */
    private void dataChanged() {
        if (mAdapter.getItemCount() == 0) {
            // 没有数据
            if (mEmptyView != null) {
                mEmptyView.setVisibility(VISIBLE);
            }
        }else{
            // 没有数据
            if (mEmptyView != null) {
                mEmptyView.setVisibility(GONE);
            }
        }
    }

}
